package com.tmall.testgen.client.runinspring.push.agent;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NeedAop {
    
    private static Map mockSnap;
    
    private static String mockDes=null;
    
    public static String getMockDes()throws Exception{
        if(mockDes!=null)
            return mockDes;
        
        Class classInfoClass=Class.forName("com.tmall.testgen.common.ClassInfo");
        Method getInstanceMethod=classInfoClass.getMethod("getInstance");
    
        Object classInfoInstance=getInstanceMethod.invoke(classInfoClass);
        
        Method getMockDesMethod=classInfoClass.getMethod("getMockDes");
        return (String) getMockDesMethod.invoke(classInfoInstance);
    }
    
    private static Map getMockSnap() throws Exception{
        if(mockSnap!=null)
            return mockSnap;
        
            Class classInfoClass=Class.forName("com.tmall.testgen.common.ClassInfo");
            Method getInstanceMethod=classInfoClass.getMethod("getInstance");
        
            Object classInfoInstance=getInstanceMethod.invoke(classInfoClass);
            
            Method mockSnapMethod=classInfoClass.getMethod("getMockSnaps");
            mockSnap=(Map) mockSnapMethod.invoke(classInfoInstance);
            
            return mockSnap;
            
        
    }

    public static Object getAopResult(String methodTotalShow,Object[] paramValues){
        System.out.println(methodTotalShow);
        try{
           Map mockSnap=getMockSnap();
           System.out.println("mockSnap:"+mockSnap);
            if(mockSnap==null||mockSnap.get(methodTotalShow)==null)
                return null;
            
            
            Object methodSnap=mockSnap.get(methodTotalShow);
            
            Method getResultMethod=methodSnap.getClass().getMethod("getResult");
            Object result=  getResultMethod.invoke(methodSnap);
            System.out.println("result:"+result);
            if(paramValues==null||paramValues.length==0)
                return result;
            
            List<Object> paramList=new ArrayList<Object>();
            for(Object obj:paramValues){
                paramList.add(obj);
            }
            
            Method getParamMethod=methodSnap.getClass().getMethod("getParams");
            List params= (List) getParamMethod.invoke(methodSnap);
            
            
            
            Object comparatorObj=Class.forName("com.tmall.testgen.common.util.Comparator").newInstance();
            Method compareMethod=comparatorObj.getClass().getMethod("compare", Object.class,Object.class);
            Boolean isEqual= (Boolean) compareMethod.invoke(comparatorObj, params,paramList);
            
            System.out.println("isEqual"+isEqual);
            if(isEqual)
                return result;
            
            return null;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
